﻿using org.xisoft.game.utils.game;
using System;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;

namespace org.xisoft.game.utils.ui
{
    public class ChessBoardControl : Control
    {
        private Loading loading1;
        private ChessBoardPanelBase chessBoard;
        protected ChessBoardPanelBase ChessBoard { get { return chessBoard; } }

        public ChessBoardControl()
        {
            DoubleBuffered = true;
            InitializeComponent();
        }

        public void Save(BinaryWriter bw)
        {
            if (chessBoard == null) return;
            bw.Write(chessBoard.GetType().AssemblyQualifiedName);
            chessBoard.Save(bw);
        }

        public void Read(BinaryReader br)
        {
            Init(br.ReadString());
            if (chessBoard != null)
                chessBoard.Read(br);
            Refresh();
        }


        public void Init(String typeName)
        {
            Type type = Type.GetType(typeName);
            ChessBoardPanelBase chessBoard = Activator.CreateInstance(type) as ChessBoardPanelBase;
            if (chessBoard == null) return;
            chessBoard.ChessBoardControl = this;
            this.chessBoard = chessBoard;

            FindForm().Text = chessBoard.Name;

            Refresh();
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (chessBoard == null) return;

            Enabled = false;
            if (e.Button == MouseButtons.Right)
                chessBoard.Undo();
            else
                chessBoard.OnMouseClick(e.X, e.Y);
            Enabled = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (chessBoard == null) return;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            chessBoard.OnPaint(e.Graphics);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            //if(ChessBoard!=null)
            //ChessBoard.OnSizeChanged(Width, Height);


            loading1.Left = (Width - loading1.Width) / 2;
            loading1.Top = (Height - loading1.Height) / 2;

            Invalidate();
        }

        private void InitializeComponent()
        {
            this.loading1 = new org.xisoft.game.utils.ui.Loading();
            this.SuspendLayout();
            // 
            // loading1
            // 
            this.loading1.Location = new System.Drawing.Point(0, 0);
            this.loading1.Name = "loading1";
            this.loading1.Size = new System.Drawing.Size(120, 120);
            this.loading1.TabIndex = 0;
            this.loading1.Text = "loading1";
            this.Controls.Add(loading1);
            this.ResumeLayout(false);

        }



        internal void ShowLoading(bool enabled)
        {
            if (enabled)
                loading1.ShowLoading();
            else
                loading1.HideLoading();
        }
    }

}
